package de.beuth.hochschule.ats.activitys;

import java.sql.SQLException;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

import de.beuth.hochschule.ats.R;
import de.beuth.hochschule.ats.entitys.DatabaseHelper;
import de.beuth.hochschule.ats.entitys.Lecture;
import de.beuth.hochschule.ats.util.LanguageMap;

public class EditLecture extends OrmLiteBaseActivity<DatabaseHelper> implements
		OnClickListener {
	private String action;
	private AutoCompleteTextView nameTextView;
	private Spinner nativeLanguageSpinner, foreignLanguageSpinner;
	private Lecture lecture;
	private AlertDialog.Builder builder;
	private AlertDialog dialog;
	private LanguageMap languageMap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("Info", "EditLecture View wurde geladen!");

		setContentView(R.layout.edit_lecture);
		
		languageMap = new LanguageMap();
		nameTextView = (AutoCompleteTextView) findViewById(R.id.lecture_name);
		nativeLanguageSpinner = (Spinner) findViewById(R.id.nativeLanguageSpinner);
		foreignLanguageSpinner = (Spinner) findViewById(R.id.foreignLanguageSpinner);
		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item,
				languageMap.getDisplayLanguages());
		spinnerArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		builder = new AlertDialog.Builder(this);
		
		findViewById(R.id.save_btn).setOnClickListener(this);
		findViewById(R.id.cancel_btn).setOnClickListener(this);
		nativeLanguageSpinner.setAdapter(spinnerArrayAdapter);
		foreignLanguageSpinner.setAdapter(spinnerArrayAdapter);

		action = getIntent().getAction();
		if (action.equals(Intent.ACTION_EDIT)) {
			int lecture_id;
			if (savedInstanceState == null) {
				lecture_id = getIntent().getExtras().getInt("lecture_id");
			} else {
				lecture_id = savedInstanceState.getInt("lecture_id");
			}
			try {
				lecture = getHelper().getLectureDao().queryForId(lecture_id);
				fillInputFields();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			setTitle(R.string.edit_lecture);
		} else {
			nativeLanguageSpinner.setSelection(languageMap
					.getDisplayLanguages().indexOf(
							Locale.getDefault().getDisplayLanguage()));
			setTitle(R.string.new_lecture);
		}

		// buildDialog();
	}

	public void onClick(View v) {

		String name = nameTextView.getText().toString().trim();
		String nativeLanguage = languageMap.get(nativeLanguageSpinner
				.getSelectedItem());
		String foreignLanguage = languageMap.get(foreignLanguageSpinner
				.getSelectedItem());

		switch (v.getId()) {
		case R.id.save_btn:
			if (name.trim().length() == 0) {
				showValidationDialog();
				return;
			}
			if (action.equals(Intent.ACTION_EDIT)) {
				try {
					lecture.update(name, nativeLanguage, foreignLanguage);
					getHelper().getLectureDao().update(lecture);
					finish();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				try {
					lecture = new Lecture();
					lecture.update(name, nativeLanguage, foreignLanguage);
					getHelper().getLectureDao().create(lecture);
					finish();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			break;
		default:
			finish();
			break;
		}

	}

	private void fillInputFields() {
		int position;
		nameTextView.setText(lecture.getName());
		position = languageMap.getIso3Languages().indexOf(lecture.getNativeLanguage());
		nativeLanguageSpinner.setSelection(position);
		position = languageMap.getIso3Languages().indexOf(lecture.getForeignLanguage());
		foreignLanguageSpinner.setSelection(position);
	}

	private void showValidationDialog() {
		builder.setMessage(R.string.no_name);
		builder.setTitle(R.string.oops);
		builder.setCancelable(false).setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		dialog = builder.create();
		dialog.show();
	}

}
